package com.ruoyi.system.service.impl;

import com.ruoyi.common.snmp.getSnmp.SNMPSessionUtil;
import com.ruoyi.system.domain.SnmpMemory;
import com.ruoyi.system.mapper.SnmpMemoryMapper;
import com.ruoyi.system.service.ISnmpMemoryService;
import org.snmp4j.PDU;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@Service
public class SnmpMemoryServiceImpl implements ISnmpMemoryService
{
    @Autowired
    private SnmpMemoryMapper snmpMemoryMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SnmpMemory selectSnmpMemoryById(Long id)
    {
        return snmpMemoryMapper.selectSnmpMemoryById(id);
    }
    @Override
    public SnmpMemory GetInfo(String ip) throws Exception {
        SNMPSessionUtil sessionUtil = new SNMPSessionUtil(ip, "161", "public", "2");
        String[] isSnmp = {".1.3.6.1.2.1.1.3"};
        ArrayList<String> isSnmpGet = sessionUtil.getIsSnmpGet(PDU.GET, isSnmp);
        String[] oids = {".1.3.6.1.2.1.25.3.3.1.2"};
        String[] oids2 = {".1.3.6.1.2.1.4.1.0"};
        String[] oids3 = {".1.3.6.1.2.1.1.1.0"};
        ArrayList<String> type = sessionUtil.getSnmpGet(PDU.GET, oids3);
        DecimalFormat df = new DecimalFormat("#.00");
        String[] pcM = {
                ".1.3.6.1.4.1.2021.4.5.0",//总的物理内存kb
                ".1.3.6.1.4.1.2021.4.6.0",//可以使用的内存kb
        };
        String[] ciscoMem = {"1.3.6.1.4.1.9.9.48.1.1.1.6.1"};//未使用
        String[] ciscoUsed = {"1.3.6.1.4.1.9.9.48.1.1.1.5.1"};//已使用
        ArrayList<String> dtype = sessionUtil.snmpWalk2(oids);
        SimpleDateFormat dftime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<String> forwording = sessionUtil.getSnmpGet(PDU.GET, oids2);
        double memTotalReal,memAvailReal,memRate=0.0;
        if ("-1".equals(isSnmpGet.get(0))) {
            SnmpMemory weiz = new SnmpMemory();
            weiz.setIp(ip);
            weiz.setMemRate("0.0");
            weiz.setMemTotalReal("0.0");
            weiz.setMemAvailReal("0.0");
            weiz.setTime(dftime.format(new Date()));
            snmpMemoryMapper.insertSnmpMemory(weiz);
            return weiz;
        } else {
            if (!dtype.isEmpty() && ("2".equals(forwording.get(0))||"1".equals(forwording.get(0)))) { //服务器
                SnmpMemory memory = new SnmpMemory();
                memory.setIp(ip);
                ArrayList<String> list = sessionUtil.getSnmpGet(PDU.GET, pcM);
                memTotalReal=Double.parseDouble(df.format(Float.parseFloat(list.get(0)) / 1024));
                memory.setMemTotalReal(String.valueOf(memTotalReal));//得到MB
                memAvailReal=Double.parseDouble(df.format(Float.parseFloat(list.get(1)) / 1024));
                memory.setMemAvailReal(String.valueOf(memAvailReal));//同上
                Double d = ((Double.parseDouble(list.get(0)) / 1024) - (Double.parseDouble(list.get(1)) / 1024)) / (Double.parseDouble(list.get(0)) / 1024);
                memRate=Double.parseDouble(df.format(d)) * 100;
                memory.setMemRate(String.valueOf(memRate));
                memory.setTime(dftime.format(new Date()));
                snmpMemoryMapper.insertSnmpMemory(memory);
                return memory;
            } else if (type.get(0).contains("Cisco")) {// cisco设备
                SnmpMemory cisco = new SnmpMemory();
                ArrayList<String> memfree = sessionUtil.getSnmpGet(PDU.GET, ciscoMem);
                ArrayList<String> memused = sessionUtil.getSnmpGet(PDU.GET, ciscoUsed);
                String free = df.format(Double.valueOf(memfree.get(0)) / 1024 / 1024); //Mb
                String used = df.format(Double.valueOf(memused.get(0)) / 1024 / 1024);//Mb
                Double total = (Double.parseDouble(used) / 1024 / 1024) + (Double.parseDouble(free) / 1024 / 1024);
                double usedRate = Double.valueOf(used) / total;
                String ur = df.format(usedRate * 100);
                cisco.setIp(ip);
                cisco.setMemAvailReal(free);
                cisco.setMemTotalReal(df.format(total));
                cisco.setMemRate(ur);
                cisco.setTime(dftime.format(new Date()));
                snmpMemoryMapper.insertSnmpMemory(cisco);
                return cisco;
            } else if (type.get(0).contains("Ruijie")) {
                String[] m = {"1.3.6.1.4.1.4881.1.1.10.2.35.1.1.1.3.1"};//内存使用率
                SnmpMemory ruijie = new SnmpMemory();
                ArrayList<String> mr = sessionUtil.getSnmpGet(PDU.GET, m);
                ruijie.setIp(ip);
                ruijie.setMemAvailReal("0.0");
                ruijie.setMemTotalReal("0.0");
                ruijie.setMemRate(mr.get(0));
                ruijie.setTime(dftime.format(new Date()));
                snmpMemoryMapper.insertSnmpMemory(ruijie);
                return ruijie;
            }else if (type.get(0).contains("Xiaomi") || type.get(0).contains("ImmortalWrt")) {
                // 假设这些是针对小米或ImmortalWrt路由器的正确OID
                String[] memOids = {
                        "1.3.6.1.2.1.25.2.3.1.6.1", // hrStorageUsed
                        "1.3.6.1.2.1.25.2.3.1.5.1"  // hrStorageSize
                };

                SnmpMemory xiaomi = new SnmpMemory();
                ArrayList<String> memValues = sessionUtil.getSnmpGet(PDU.GET, memOids);

                // 计算总内存和可用内存
                Double usedMemory = Double.valueOf(memValues.get(0)) / 1024; // 转换为MB
                Double totalMemory = Double.valueOf(memValues.get(1)) / 1024; // 转换为MB
                Double freeMemory = totalMemory - usedMemory;

                // 计算使用率
                double usedRate = usedMemory / totalMemory;
                String ur = df.format(usedRate * 100);

                xiaomi.setIp(ip);
                xiaomi.setMemAvailReal(String.valueOf(freeMemory));
                xiaomi.setMemTotalReal(String.valueOf(totalMemory));
                xiaomi.setMemRate(ur);
                xiaomi.setTime(dftime.format(new Date()));
                snmpMemoryMapper.insertSnmpMemory(xiaomi);
                return xiaomi;
            }

        }
        SnmpMemory weiz = new SnmpMemory();
        weiz.setIp(ip);
        weiz.setMemRate("0.0");
        weiz.setMemTotalReal("0.0");
        weiz.setMemAvailReal("0.0");
        weiz.setTime(dftime.format(new Date()));
        return weiz;

    }
    /**
     * 查询【请填写功能名称】列表
     *
     * @param snmpMemory 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SnmpMemory> selectSnmpMemoryList(SnmpMemory snmpMemory)
    {
        return snmpMemoryMapper.selectSnmpMemoryList(snmpMemory);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param snmpMemory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSnmpMemory(SnmpMemory snmpMemory)
    {
        return snmpMemoryMapper.insertSnmpMemory(snmpMemory);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param snmpMemory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSnmpMemory(SnmpMemory snmpMemory)
    {
        return snmpMemoryMapper.updateSnmpMemory(snmpMemory);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSnmpMemoryByIds(Long[] ids)
    {
        return snmpMemoryMapper.deleteSnmpMemoryByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSnmpMemoryById(Long id)
    {
        return snmpMemoryMapper.deleteSnmpMemoryById(id);
    }
    @Override
    public List<SnmpMemory> GetMemWithFive(String ip) {
        return snmpMemoryMapper.GetMemWithFive(ip);
    }
    @Override
    public SnmpMemory GetMemWithOne(String ip) {
        return snmpMemoryMapper.GetMemWithOne(ip);
    }

}
